﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using HHBLL;
using HHModel;
using HHCommon;

namespace HHWeb.SystemManage.Users
{
    public partial class Dispose : Base
    {
        HHCommon.Help help = new Help();
        EncAndDec EncAndDec = new EncAndDec();
        HHModel.Users M = new HHModel.Users();
        HHBLL.Users B= new HHBLL.Users();
        //定义变量
        public string UserNumber =string.Empty;
        public string UserName = string.Empty;
        public string UserCode = string.Empty;
        public string UserPassWord = string.Empty;
        public string UserPicUrl = string.Empty;
        public string UserTel = string.Empty;
        public string UserEmail = string.Empty;
        public string UserType = string.Empty;
        public int OpenState =1;//用户状态（0：禁用1：启用）
        public int Sort =0;//排序号
        public string Signature = string.Empty;
        public string UserAddress = string.Empty;        
        public string Creater = string.Empty;
        public string CreateTime = string.Empty;
        public string Command = "Add";
        protected void Page_Load(object sender, EventArgs e)
        {
            string message = string.Empty;
            try
            {
                if (EncAndDec.DESDecrypting(help.GetCookie("UserType")) != "1")
                {
                    MessageBox.ResponseScript(this.Page, "对不起！没有操作权限", "javascript:history.go(-1)");
                    return;
                }
                if (help.GetHttpMethodBool())
                {
                    switch (Request.Form["Command"].ToString())
                    {
                        case "Add":
                            message = AddUser();
                            break;
                        case "Update":
                            message = UpdateUser();
                            break;
                        default:
                            break;
                    }
                    if (message.EndsWith("成功！"))
                        MessageBox.ShowAndRedirect(this.Page, message, "List.aspx");
                    else
                        MessageBox.ResponseScript(this.Page, message, "javascript:history.go(-1)");
                }
                else
                {
                    if (help.IFPageParameter())
                    {
                        int userid = int.Parse(Request.QueryString["ID"].ToString());
                        M = B.GetModel(userid);
                        if (M != null)
                        {
                            UserNumber = M.UserNumber;
                            UserName = M.UserName;
                            UserCode = M.UserCode;
                            Signature = M.Signature;
                            UserPicUrl = "../../MYFiles/UserImg/"+M.UserPicUrl;
                            UserTel = M.UserTel;
                            UserEmail = M.UserEmail;
                            UserType = M.UserType;
                            OpenState = Convert.ToInt32(M.OpenState);
                            Sort = Convert.ToInt32(M.Sort);
                            UserAddress = M.UserAddress;
                            Creater = M.Creater;
                            CreateTime = Convert.ToDateTime(M.CreateTime).ToString("yyyy-MM-dd HH:mm:ss");
                            Command = "Update";
                        }
                        else
                        {
                            MessageBox.ShowAndRedirect(this.Page, "该数据不存在！", "List.aspx");
                            return;
                        }
                    }
                    else
                    {
                        UserNumber = "U-" + help.GetRandom();
                        Creater = EncAndDec.DESDecrypting(help.GetCookie("UserCode"));
                        CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    }
                }
            }
            catch
            {
                MessageBox.ResponseScript(this.Page, "操作错误，请联系管理员！", "javascript:history.go(-1)");
                return;
            }
        }

        #region //添加用户
        public string AddUser()
        {
            M.UserNumber = Request.Form["UserNumber"].ToString();
            M.UserName = Request.Form["UserName"].ToString();
            M.UserCode = Request.Form["UserCode"].ToString();
            M.UserPassWord = help.GetMD5(Request.Form["UserPassWord"].ToString());
            #region //图片上传
            HttpPostedFile hpf = upload_img.PostedFile;
            string fileName = Path.GetFileName(hpf.FileName);
            string ex = Path.GetExtension(fileName);//获取文件后缀名
            if (string.IsNullOrEmpty(fileName))
                return "上传图片不能为空！";
            string SavaUrl = string.Empty;
            string rd = help.GetRandom(); 
            if (ex == ".gif" || ex == ".jpg" || ex == ".bmp" || ex == ".png")
            {
                SavaUrl = "../../MYFiles/UserImg/" +rd+ ex;
                hpf.SaveAs(Server.MapPath(SavaUrl));//保存图片路经
            }
            else
                return "图片格式错误！";
            #endregion
            M.UserPicUrl =rd+ex;
            M.UserTel = Request.Form["UserTel"].ToString();
            M.UserEmail = Request.Form["UserEmail"].ToString();
            M.UserType = Request.Form["UserType"].ToString();
            M.OpenState =Convert.ToInt32(Request.Form["OpenState"]);
            M.RegState =0;
            M.Sort = int.Parse(Request.Form["Sort"].ToString());
            M.Signature = Request.Form["Signature"].ToString();
            M.UserAddress = Request.Form["UserAddress"].ToString();
            M.Creater = Request.Form["Creater"].ToString();
            M.CreateTime = DateTime.Now;
            M.LastUpdater = EncAndDec.DESDecrypting(help.GetCookie("UserCode"));
            M.LastUpdateTime = DateTime.Now;
            if (B.Add(M) > 0)
                return "添加成功！";
            else
                return "添加失败！";
        }
        #endregion

        #region //修改用户
        public string UpdateUser()
        {
            M.ID = int.Parse(Request.QueryString["ID"].ToString());
            M.UserNumber = Request.Form["UserNumber"].ToString();
            M.UserName = Request.Form["UserName"].ToString();
            M.UserCode = Request.Form["UserCode"].ToString();
            #region //图片上传
            HttpPostedFile hpf = upload_img.PostedFile;
            string fileName = Path.GetFileName(hpf.FileName);
            string ex = Path.GetExtension(fileName);//获取文件后缀名
            string SavaUrl = string.Empty;
            string rd = help.GetRandom();
            string OldImgUrl = Request.Form["ImgUrl"].ToString();
            if (!string.IsNullOrEmpty(fileName))
            {
                if (ex == ".gif" || ex == ".jpg" || ex == ".bmp" || ex == ".png")
                {
                    SavaUrl = "../../MYFiles/UserImg/" +rd+ ex;
                    if (System.IO.File.Exists(Server.MapPath(OldImgUrl)))
                        System.IO.File.Delete(Server.MapPath(OldImgUrl));
                    hpf.SaveAs(Server.MapPath(SavaUrl));//保存图片路经
                }
                else
                    return "图片格式错误！";
                M.UserPicUrl =rd+ex;
            }
            else
                M.UserPicUrl = OldImgUrl.Remove(0,OldImgUrl.LastIndexOf("/")+1);
            #endregion
            M.UserTel = Request.Form["UserTel"].ToString();
            M.UserEmail = Request.Form["UserEmail"].ToString();
            M.UserType = Request.Form["UserType"].ToString();
            M.OpenState = Convert.ToInt32(Request.Form["OpenState"]);
            M.RegState =0;
            M.Sort = int.Parse(Request.Form["Sort"].ToString());
            M.Signature = Request.Form["Signature"].ToString();
            M.UserAddress = Request.Form["UserAddress"].ToString();
            M.Creater = Request.Form["Creater"].ToString();
            M.CreateTime = Convert.ToDateTime(Request.Form["CreateTime"].ToString());
            M.LastUpdater =EncAndDec.DESDecrypting(help.GetCookie("UserCode"));
            M.LastUpdateTime = DateTime.Now;
            if (B.UpdateUserInfo(M))
                return "修改成功！";
            else 
                return "修改失败！";
        }
        #endregion
    }
}